<template>
  <div class="tab-bar-item" v-on="{click:itemClick,dblclick:itemClick}">
    <div :style="activeStyle">
      <slot name="item-icon"></slot>
    </div>
    <div :style="activeStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    path: String,
    name: String,
    activeColor: {
      type: String,
      default: "#ea986c",
    },
  },
  methods: {
    itemClick () {
      if (this.name) {
        if (this.$route.name !== this.name) {
          this.$router.replace({ name: this.name })
        }
      } else if (this.path) {
        if (this.$route.path !== this.path) {
          this.$router.replace(this.path)
        }
      } else {
        throw new Error("请为tab-bar-item传入路由name或path")
      }
    },
  },
  computed: {
    isActive () {
      if (this.name) {
        return this.$route.name == this.name
      } else {
        return this.$route.path.indexOf(this.path) !== -1
      }
    },
    activeStyle () {
      return this.isActive ? { color: this.activeColor } : {}
    },
  },
};
</script>

<style scoped lang="less">
.tab-bar-item {
  flex: 1;
  text-align: center;
  .iconfont {
    font-size: 0.4rem;
    vertical-align: middle;
  }
}
</style>